/* 852. 山脉数组的峰顶索引 */
/**
 * @param {number[]} arr
 * @return {number}
 */
/* 枚举 */
var peakIndexInMountainArray = function (arr) {
	const n = arr.length
	let ans = -1
	for (let i = 1; i < n - 1; i++) {
		if (arr[i] < arr[i + 1]) {
			ans = i
			break
		}
	}
	return ans
}
/* 
    二分查找
    
    当 i < j 时 arr[i] < arr[i+1] 恒成立
    当 i >= j arr[i] > arr[i+1]
*/
var peakIndexInMountainArray = function (arr) {
	const n = arr.length
	let left = 1,
		right = n - 2,
		ans = 0
	while (left <= right) {
		const mid = left + ((right - left) >> 1)
		if (ans[mid] > ans[mid + 1]) {
			ans = mid
			right = mid - 1
		} else {
			left = mid - 1
		}
	}

	return ans
}
const index = peakIndexInMountainArray([0, 10, 5, 2])
console.log('peakIndexInMountainArray :>>', index)
